package icedot.library.common.ui;


import icedot.library.common.R;
import icedot.library.common.base.Logger;
import icedot.library.common.utils.CommonFun;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 控件的排列为：ImageView TextView TextView ImageView
 * 点击时整行响应
 * @author root
 *
 */

public class GuideButton extends LinearLayout
{
	private LinearLayout 	_layoutItem;
	private ImageView		_imageLeft;
	private ImageView 		_imageRight;
	private TextView		_textMiddle;
	private TextView		_textOther;

	public GuideButton(Context context) 
	{
		super(context);
		initView(context);
	}
	
	public GuideButton(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		initView(context);
	}
	
	private void initView(Context context)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);  
		inflater.inflate(R.layout.btn_guide, this);
		
		_layoutItem = (LinearLayout)findViewById(R.id.btnguide_layout_item);
		_imageLeft = (ImageView)findViewById(R.id.btnguide_image_left);
		_textMiddle = (TextView)findViewById(R.id.btnguide_text_middle);
		_textOther = (TextView)findViewById(R.id.btnguide_text_other);
		_imageRight = (ImageView)findViewById(R.id.btnguide_image_right);
		
		_layoutItem.setClickable(true);
	}
	
	/**
	 * 设置文字和字体颜色
	 * @param title
	 * @param color
	 */
	public void setText(String title,int color)
	{
		if(_textMiddle == null)
		{
			Logger.showDebugMsg("_textMiddle is null");
			return;
		}
		_textMiddle.setText(title);
		_textMiddle.setTextColor(color);
	}
	
	public void setText(String title)
	{
		if(_textMiddle == null)
		{
		    Logger.showDebugMsg("_textMiddle is null");
			return;
		}
		_textMiddle.setText(title);
	}
	
	public String getText()
	{
		return _textMiddle.getText().toString();
	}
	
	public void setLeftImage(int resId)
	{
		if(_imageLeft == null)
		{
		    Logger.showDebugMsg("_imageLeft is null");
			return;
		}
		if(resId == 0)
		{
			_imageLeft.setVisibility(View.GONE);
		}
		else
		{
			_imageLeft.setImageResource(resId);
		}
	}
	
	public void setRightImage(int resId)
	{
		if(_imageRight == null)
		{
		    Logger.showDebugMsg("_imageRight is null");
			return;
		}
		if(resId == 0)
		{
			_imageRight.setVisibility(View.GONE);
		}
		else
		{
			_imageRight.setImageResource(resId);
		}
	}
	
	public TextView getMiddleTextView()
	{
		return _textMiddle;
	}
	
	public TextView getOtherTextView()
	{
		return _textOther;
	}
	
	public ImageView getLeftImageView()
	{
		return _imageLeft;
	}
	
	public ImageView getRightImageView()
	{
		return _imageRight;
	}
	@Override
	public void setBackgroundResource(int resid) 
	{
		_layoutItem.setBackgroundResource(resid);
	}
	
	@Override
	public void setOnClickListener(OnClickListener l) 
	{
		_layoutItem.setOnClickListener(l);
	}
	
	/**
	 * 常用配置
	 * 2边的图留边距 中间的文字在左
	 */
	public void defaultSettings()
	{
		float margindp = 10;
		int marginpx = CommonFun.dip2px(this.getContext(), margindp);
		
		LinearLayout.LayoutParams ps = (LinearLayout.LayoutParams)_imageLeft.getLayoutParams();
		ps.setMargins(marginpx, marginpx, marginpx, marginpx);
		_imageLeft.setLayoutParams(ps);
		
		LinearLayout.LayoutParams ps2 = (LinearLayout.LayoutParams)_imageRight.getLayoutParams();
		ps2.setMargins(marginpx, marginpx, marginpx, marginpx);
		_imageRight.setLayoutParams(ps2);
	}
	
	/**
	 * 常用配置2
	 * 左图不显示,右图显示,中间文字顶满,other为wrap
	 */
	public void defaultSettings2()
	{
		int left = CommonFun.dip2px(this.getContext(), 15);
		int right = CommonFun.dip2px(this.getContext(), 10);
		int top = CommonFun.dip2px(this.getContext(), 0);
		int bottom = CommonFun.dip2px(this.getContext(), 0);
		
		LinearLayout.LayoutParams ps = (LinearLayout.LayoutParams)_imageLeft.getLayoutParams();
		ps.setMargins(left, top, right, bottom);
		_imageLeft.setLayoutParams(ps);
		
		LinearLayout.LayoutParams ps2 = (LinearLayout.LayoutParams)_imageRight.getLayoutParams();
		ps2.setMargins(right, top, right, bottom);
		_imageRight.setLayoutParams(ps2);
	}
	
	/**
	 * 常用配置3
	 * 左图不显示,右图不显示,中间文字为wrap,other顶满
	 */
	public void defaultSettings3()
	{
		float margindp = 15;
		int marginpx = CommonFun.dip2px(this.getContext(), margindp);
		
		LinearLayout.LayoutParams ps1 = (LinearLayout.LayoutParams)_textMiddle.getLayoutParams();
		ps1.setMargins(marginpx, 0, 0, 0);
		ps1.weight = 0;
		_textMiddle.setLayoutParams(ps1);
		
		LinearLayout.LayoutParams ps2 = (LinearLayout.LayoutParams)_textOther.getLayoutParams();
		ps2.setMargins(marginpx, 0, marginpx, 0);
		ps2.weight = 1;
		_textOther.setLayoutParams(ps2);
	}
}
